<?php
	function startAttack($ini1, $ini2)
	{
		$salir = false;
		//alimentamos el generador de aleatorios
		srand (time());		  
		while ( $salir == false )
		{
			//generamos dos número aleatorios
			$n_a = rand(1,10) + $ini1; //JugA
			$n_b = rand(1,10) + $ini2; //JugB
			
			if ( $n_a > $n_b ) //JugA starts
			{
				$salir = true;
				$jug = 0; 
			} 
			else if ( $n_a < $n_b ) //JugB starts
			{
				$salir = true;
				$jug = 1;
			}
		}
		return $jug;
	}
	
	function sucessfulAttack($skillNameAtk, $skillAtk, $skillDef, $styleAtk, $styleDef, $velAtk, $strDef, $agiDef)
	{
		//alimentamos el generador de aleatorios
		srand (time());		  
			
		$atk = rand(1,20) + $skillAtk; //JugA
		$def = rand(1,20) + $skillDef; //JugB
		
		// Estilos de atake
		switch ($styleAtk)
		{
			case "Outfighter":
				if ( $skillNameAtk == "Jab" )
				{
					$modif = 1;
				}
				else if ( $skillNameAtk == "Directo" )
				{
					$modif = 0.75;
				}
				else
				{
					$modif = 0.5;
				}
				break;
			case "Puncher":
				$modif = 0.75;
				break;
			case "Infighter":
				if ( $skillNameAtk == "Jab" )
				{
					$modif = 0.5;
				}
				else if ( $skillNameAtk == "Directo" )
				{
					$modif = 0.75;
				}
				else if ( $skillNameAtk == "Uppercut" || $skillNameAtk == "Hook" )
				{
					$modif = 1;
				}
				break;
				
		}
		$atk += $velAtk * $modif;
		
		// Estilos de defensa
		switch ($styleDef)
		{
			case "Peekaboo":
				if ( $skillNameAtk == "Uppercut" )
				{
					$modif = 0.25;
				}
				else if ( $skillNameAtk == "Hook" )
				{
					$modif = 1.5;
				}
				else
				{
					$modif = 0.75;	
				}
				break;
			case "Cruz":
				if ( $skillNameAtk == "Uppercut" )
				{
					$modif = 1.5;
				}
				else if ( $skillNameAtk == "Hook" )
				{
					$modif = 0.25;
				}
				else
				{
					$modif = 0.75;	
				}
				break;
			case "Cangrejo":
			case "Footplay":
				$modif = 0.75;
				break;
			case "Esquiva":
				if ( $skillNameAtk == "Uppercut" || $skillNameAtk == "Hook" )
				{
					$modif = 0.5;
				}
				else
				{
					$modif = 0.75;
				}
				break;
				
		}		
		// Modificacion por habilidad de defensa
		if ( $styleDef == "Cruz" || $styleDef == "Peekaboo" )	
 			$def += $strDef * $modif;
		else
			$def += $agiDef * $modif;
			
		if ( $atk > $def ) //ATk wins
		{
			$success = 1; 
		} 
		else if ( $atk <= $def ) //ATK loses
		{
			$success = 0;
		}
		return $success;
		//return 1;
	}
	
	function fight($strJ, $enemyHPcurrent, $enemyName)
	{
		srand (time());
		//generamos dos número aleatorios
		$dmg = rand(1,10) + $strJ; //JugA
		return $dmg;
	}
	
	function checkAttackAfterFail()
	{
		srand (time());
		$val = rand(1,20);
		
		return $val;
	}
	
	function addTime($min, $max)
	{
		srand (time());
		$val = rand($min,$max);
		return $val;
	}
	
	function checkHitType()
	{
		srand (time());
		$val = rand(1,100);
		
		//Vemos a que golpe pertenece		
		if ( $val > 50 )
		{
			if ( $val > 80 )
			{
				if ( $val > 95 )
				{
					$res = 4;
				}
				else
				{
					$res = 3;
				}
			}
			else
			{	
				$res = 2;
			}
		}
		else
		{
			$res = 1;
		}	
		return $res;
	}
	
	function checkDefType()
	{
		srand (time());
		$val = rand(1,100);
		
		//Vemos a que golpe pertenece		
		if ( $val > 50 )
		{
			if ( $val > 80 )
			{
				if ( $val > 95 )
				{
					$res = 4;
				}
				else
				{
					$res = 3;
				}
			}
			else
			{	
				$res = 2;
			}
		}
		else
		{
			$res = 1;
		}	
		return $res;
	}

	function checkStrengths($strClincher, $strObjective)
	{
		srand (time());
		if ( $strClincher > $strObjective )
			$clinch = 1;
		else if (  $strClincher < $strObjective )
			$clinch = 0;
		else
		{
			$val = rand(1,100);
			if ( $val > 50 ) $clinch = 1;
			else 
				$clinch = 0;
		}
		return $clinch;
	}
	
	function calc_pic_timer($tiempo)
	{
		$cdisp = $tiempo; // Storing the counter value in another variable	
		$cadena = "<60";
		if ( $cdisp >= 60 )
		{
			$mins = floor( $cdisp / 60 );
			$secs = $cdisp % 60;
			
			if ( $secs < 10 )
				$extra = "0";
			else
				$extra = "";
			
			$cadena = "".$mins."".$extra.$secs."";
			$cdisp = (int)$cadena;
		}
	/*	echo "<br>*******************************<br>";
		echo "[DEBUG] $cadena: $mins:$secs - $cdisp";
		echo "<br>*******************************<br>";*/
		$divisor=10; // setting the divisor value to 10
		$digitarray=array(); // creating an array
		
		do 
		{
			$digit=($cdisp % $divisor); // looping through the till all digits are taken
			$cdisp=($cdisp/$divisor); // getting the digits from right side
			array_push($digitarray,$digit); // storing them in the array
		} 
		while ( $cdisp >=1 ); // condition of do loop
		
		// array is to be reversed as digits are in reverse order
		$digitarray = array_reverse($digitarray); 		
		
		return $digitarray;
		
	/*	while (list ($key, $val) = each ($digitarray))  // looping through the array
		{
			echo "<img src='digits/$dir/$val.gif'>"; 
		// calling one by one digits based on the value of the array
		} // end of the loop 	
		
		*/
	}
?>